# pip install transformers torch

from transformers import GPT2LMHeadModel, GPT2Tokenizer

# 加载预训练的GPT-2模型和分词器
model_name = "distilgpt2"

# 下载模型和分词器
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

# 将模型移动到 CPU
model.to("cpu")

# 输入一个文本
input_text = "From long long ago in a galaxy"

# 将输入文本转化为模型可处理的格式
input_ids = tokenizer.encode(input_text, return_tensors="pt")

# 使用模型进行生成
output = model.generate(input_ids, max_length=50, num_return_sequences=1)

# 解码生成的文本
output_text = tokenizer.decode(output[0], skip_special_tokens=True)

print("Generated Text:")
print(output_text)
